# -*- coding: utf-8 -*-
u"""
命令行传参数的用法，感谢下边url中@alecxe的回答.

https://stackoverflow.com/questions/31527080/pass-file-name-argument-to-pipeline-for-csv-export-in-scrapy
"""
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from diana.items import DianaItem


class DianaPipeline(object):
    u"""用来保存爬取结果的pipeline."""

    @classmethod
    def from_crawler(cls, crawler):
        u"""从命令中提取相应的参数."""
        settings = crawler.settings
        file_name = settings.get("FILE_NAME")
        return cls(file_name)

    def __init__(self, file_name):
        u"""追加模式打开文件."""
        self.writer = open(file_name, 'a+')

    def process_item(self, item, spider):
        u"""写入文件."""
        if isinstance(item, DianaItem):
            labels = ['mirna_name', 'tss', 'tissue']
            row = '\t'.join([item[x] for x in labels])

            self.writer.write(row + '\n')
            return item

    def close_spider(self, spider):
        self.writer.close()
